package com.cruisecontrol4eclipse.core;

import org.eclipse.ui.PlatformUI;

import com.cruisecontrol4eclipse.core.model.CruiseControlProject;

public class PersistedData {

	public static final String HTTP_PORT = "httpPort";
	public static final String RMI_PORT = "rmiPort";
	public static final String HOSTNAME = "hostname";
	public static final String WEB_SVN_BLAME_URL = "webSvnBlameUrl";
	public static final String WEB_SVN_REVISION_URL = "webSvnRevisionUrl";
	public static final String FROM_SVN_REV_TAG = "[fromSvnRev]";
	public static final String TO_SVN_REV_TAG = "[toSvnRev]";
	public static final String SVN_REV_TAG = "[svnRev]";
	public static final String SVN_REV_MINUS_1_TAG = "[svnRev-1]";
	
	public static String getHostname() {
		return PlatformUI.getPreferenceStore().getString(HOSTNAME);
	}
	
	public static String getWebSvnBlameUrl() {
		return PlatformUI.getPreferenceStore().getString(WEB_SVN_BLAME_URL);
	}
	
	public static String getWebSvnRevisionUrl() {
		return PlatformUI.getPreferenceStore().getString(WEB_SVN_REVISION_URL);
	}
	
	public static int getHttpPort() {
		return PlatformUI.getPreferenceStore().getInt(HTTP_PORT);
	}
	
	public static int getRmiPort() {
		return PlatformUI.getPreferenceStore().getInt(RMI_PORT);
	}

	public static String getTranslatedWebSvnBlameUrl(CruiseControlProject project) {
		String result = getWebSvnBlameUrl();
		result = result.replace(FROM_SVN_REV_TAG, ""+ (Integer.parseInt(project.getLastSuccessfulBuildLabel() == null ? "0":project.getLastSuccessfulBuildLabel())+1));
		result = result.replace(TO_SVN_REV_TAG, project.getLabel());
		return result;
	}
	
	public static String getTranslatedWebSvnRevisionUrl(CruiseControlProject project) {
		String result = getWebSvnRevisionUrl();
		result = result.replace(SVN_REV_MINUS_1_TAG, ""+ (Integer.parseInt(project.getLabel() == null ? "1":project.getLabel())-1));
		result = result.replace(SVN_REV_TAG, project.getLabel());
		return result;
	}

	public static String getTranslatedWebInterfaceUrl(CruiseControlProject project) {
		return "http://"+getHostname()+":"+getHttpPort()+"/cruisecontrol/buildresults/"+project.getName();
	}
}
